package com.example.zhiran.service.tagservice;

import com.example.zhiran.mapper.ZRTagMapper;
import com.example.zhiran.pojo.front.FrontTag;
import com.example.zhiran.pojo.option.OptionResult;
import com.example.zhiran.pojo.option.tagoption.TagOption;
import com.example.zhiran.pojo.option.tagoption.TagOptionMessage;
import com.example.zhiran.pojo.orm.ZRTag;
import com.example.zhiran.service.OptionResultBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ZRTagService {
    @Autowired
    private ZRTagMapper tagMapper;

    private OptionResult getQueryOptionResult(List<ZRTag> tags){
        OptionResultBuilder optionResultBuilder = new OptionResultBuilder();
        return optionResultBuilder.setStatus(OptionResult.SUCCESS)
                .setOptionCode(TagOption.TAG_QUERY.getOptionCode())
                .setResult(tags)
                .setMessageCode(TagOptionMessage.TAG_QUERY_SUCCESS.getMessageCode())
                .build();
    }

    //查询所有tag
    public OptionResult getAllTags(){
        List<ZRTag> tags = tagMapper.getAllTags();
        return getQueryOptionResult(tags);
    }

    //根据ctyId查询tag
    public OptionResult getTagsByCategoryId(int ctyId){
        List<ZRTag> tags = tagMapper.getTagsByCategoryId(ctyId);
        return getQueryOptionResult(tags);
    }

    //判断一个分类中tagName是否重复了
    public Boolean isTagNameRepeat(String tagName,int ctyId){
        ZRTag tag = tagMapper.getSameTagNameInCategory(tagName, ctyId);
        return tag != null;
    }
    public OptionResult addTag(FrontTag tag){
        OptionResultBuilder optionResultBuilder
                = new OptionResultBuilder().setOptionCode(TagOption.TAG_ADD.getOptionCode());
        if (isTagNameRepeat(tag.getTagName(),tag.getCtyId())){        //同分类中有同名tag
            return optionResultBuilder
                    .setStatus(OptionResult.FAILURE)
                    .setMessageCode(TagOptionMessage.TAG_REPEAT.getMessageCode())
                    .build();
        }else {
            int addResult = tagMapper.addTag(tag);
            if (addResult==0){  //数据库插入失败
                return optionResultBuilder
                        .setStatus(OptionResult.FAILURE)
                        .setMessageCode(TagOptionMessage.TAG_ADD_FAIL.getMessageCode())
                        .build();
            }
            //添加成功
            return optionResultBuilder
                    .setStatus(OptionResult.SUCCESS)
                    .setMessageCode(TagOptionMessage.TAG_ADD_SUCCESS.getMessageCode())
                    .build();
        }
    }

    public OptionResult updateTag(ZRTag zrTag){
        OptionResultBuilder optionResultBuilder
                = new OptionResultBuilder().setOptionCode(TagOption.TAG_UPDATE.getOptionCode());
        if (isTagNameRepeat(zrTag.getTagName(),zrTag.getCtyId())){      //同分类中有同名tag
            return optionResultBuilder
                    .setStatus(OptionResult.FAILURE)
                    .setMessageCode(TagOptionMessage.TAG_REPEAT.getMessageCode())
                    .build();
        }else {
            int updateResult = tagMapper.updateTag(zrTag);
            if (updateResult==0){       //数据库修改失败
                return optionResultBuilder
                        .setStatus(OptionResult.FAILURE)
                        .setMessageCode(TagOptionMessage.TAG_UPDATE_FAIL.getMessageCode())
                        .build();
            }
            return optionResultBuilder
                    .setStatus(OptionResult.SUCCESS)
                    .setMessageCode(TagOptionMessage.TAG_UPDATE_SUCCESS.getMessageCode())
                    .build();
        }
    }

    public OptionResult deleteTag(int tagId){
        OptionResultBuilder optionResultBuilder
                = new OptionResultBuilder().setOptionCode(TagOption.TAG_DELETE.getOptionCode());
        int deleteResult = tagMapper.deleteTag(tagId);
        if (deleteResult==0){   //数据库删除失败
            return optionResultBuilder
                    .setStatus(OptionResult.FAILURE)
                    .setMessageCode(TagOptionMessage.TAG_DELETE_FAIL.getMessageCode())
                    .build();
        }
        return optionResultBuilder
                .setStatus(OptionResult.SUCCESS)
                .setMessageCode(TagOptionMessage.TAG_DELETE_SUCCESS.getMessageCode())
                .build();
    }
}
